/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.PeerGroupAdvertisement;

/**
 *
 * @author cs47402
 */
public class GroupAdvertisor extends Thread {
    private PeerGroupAdvertisement TheAdvertisement;
    private PeerGroup TheNetPeerGroup;

    public GroupAdvertisor(PeerGroup TheNetPeerGroup, PeerGroupAdvertisement TheAdvertisement) {
        this.TheAdvertisement = TheAdvertisement;
        this.TheNetPeerGroup = TheNetPeerGroup;

    }

    public void run() {
        String title = "XML Advertisement for PeerGroup Advertisement";
        String separator = "";
        for(int i = 0; i < title.length() + 4; i++) {
            separator = separator + "-";
        }
        while(true) {
            try {
                Thread.sleep(10000);
            } catch(Exception e) {
                // ignored
            }
            try {
                TheNetPeerGroup.getDiscoveryService().publish(TheAdvertisement,
                                        PeerGroup.DEFAULT_LIFETIME, PeerGroup.DEFAULT_EXPIRATION);
                TheNetPeerGroup.getDiscoveryService().remotePublish(TheAdvertisement,
                                        PeerGroup.DEFAULT_EXPIRATION);
            } catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("Group advertisement republished");

            System.out.println(separator);
            System.out.println("| " + title + " |");
            System.out.println(separator);

            System.out.println(TheAdvertisement.toString());

            System.out.println(separator);
        }
    }
}
